package com.lxx.modules.sys.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableMap;
import com.lxx.annotation.PermissionName;
import com.lxx.common.BaseAction;
import com.lxx.modules.sys.biz.ISysPermissionBiz;
import com.lxx.modules.sys.biz.ISysRoleBiz;
import com.lxx.modules.sys.entity.SysRole;
import com.lxx.utils.Constant;
import com.lxx.utils.DataTableResult;
import com.lxx.utils.NetworkUtil;
import com.lxx.utils.PageInfo;
import com.lxx.utils.Pagination;
import com.lxx.utils.Result;
import com.lxx.utils.ValidationUtils;
import com.lxx.vo.UserRoleData;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController extends BaseAction{
	private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleController.class);
	
	@Autowired
	private ISysRoleBiz sysRoleBiz;
	
	@Autowired
	private ISysPermissionBiz sysPermissionBiz;
	
	@RequestMapping("/query")
	@ResponseBody
	@PermissionName("查询")
	public Result query(PageInfo pageInfo, HttpServletRequest request) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String beginStr = request.getParameter("begin");
		String endStr = request.getParameter("end");
		Map<String, Object> params = new HashMap<>();
		try {
			if (StringUtils.isNotBlank(beginStr)) {
				params.put("begin", format.parse(beginStr));
			}
			if (StringUtils.isNotBlank(endStr)) {
				params.put("end", format.parse(endStr));
			}
			params.put("keywords", request.getParameter("keywords"));
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		int pageIndex = pageInfo.getStart() / pageInfo.getLength() + 1;
		Pagination<SysRole> pagination = sysRoleBiz.query(params, pageIndex, pageInfo.getLength());
		DataTableResult dataTableResult = new DataTableResult(pageInfo.getDraw(), 
				pagination.getTotalRow(), pagination.getTotalRow(), pagination.getDatas());
		return Result.success(dataTableResult);
	}
	
	@RequestMapping("/queryAll")
	@ResponseBody
	@PermissionName("查询全部")
	public Result queryAll() {
		List<UserRoleData> data = sysRoleBiz.queryAll();
		return Result.success(data);
	}
	
	@RequestMapping("/add")
	@ResponseBody
	@PermissionName("新增")
	public Result add(SysRole sysRole) {
		LOGGER.debug("{}", sysRole);
		sysRole.setDelFlag(false);
		sysRole.setCreateTime(new Date());
		ValidationUtils.validateForce(sysRole);
		sysRoleBiz.save(sysRole);
		
		// 角色权限关联
		sysRoleBiz.updateRolePermission(sysRole.getId(), sysRole.getPermIds());
		return Result.SUCCESS;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	@PermissionName("修改")
	public Result update(SysRole sysRole) {
		sysRoleBiz.update(sysRole);
		
		// 角色权限关联
		sysRoleBiz.updateRolePermission(sysRole.getId(), sysRole.getPermIds());
		return Result.SUCCESS;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	@PermissionName("删除")
	public Result delete(String ids) {
		sysRoleBiz.delete(ids);
		return Result.SUCCESS;
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	@PermissionName("详情")
	public Result detail(Integer id) {
		SysRole sysRole = sysRoleBiz.detail(id);
		return Result.success(sysRole, "rolePerms", sysPermissionBiz.getRolePermissions(id));
	}

}
